package HomeWork20241108;

import java.util.Scanner;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Work241108
{
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a string to encrypt:");
        String plaintext = scanner.nextLine();
        String ciphertext = encrypt(plaintext, 5);
        System.out.println("Encrypted: " + ciphertext);

        System.out.println("Enter a string to decrypt:");
        String decryptedText = decrypt(scanner.nextLine(), 5);
        System.out.println("Decrypted: " + decryptedText);
    }

    public static String encrypt(String text, int shift) {
        return transform(text, shift);
    }

    public static String decrypt(String text, int shift) {
        return transform(text, -shift);
    }

    private static String transform(String text, int shift) {
        StringBuilder result = new StringBuilder();
        for (char character : text.toCharArray()) {
            if (Character.isLetter(character)) {
                char base = Character.isUpperCase(character) ? 'A' : 'a';
                int offset = (character - base + shift) % 26;
                if (offset < 0) offset += 26; // For negative shifts
                result.append((char) (base + offset));
            } else {
                result.append(character);
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        DateTimeFormatter shortFormatter = DateTimeFormatter.ofPattern("MM-dd");
        DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate today = LocalDate.now(); // 将 today 定义在 try 块外部

        // 生日提醒功能
        System.out.println("请输入生日（格式：月月-日日）：");
        String birthdayStr = scanner.nextLine();
        try {
            LocalDate birthday = LocalDate.parse("2000-" + birthdayStr, fullFormatter)
                    .withYear(today.getYear());

            // 如果生日已经过了，则计算下一年的生日
            if (today.isAfter(birthday)) {
                birthday = birthday.plusYears(1);
            }

            long daysUntilBirthday = ChronoUnit.DAYS.between(today, birthday);
            if (daysUntilBirthday == 0) {
                System.out.println("今天是生日！");
            } else {
                System.out.println("距离生日还有：" + daysUntilBirthday + "天");
            }
        } catch (Exception e) {
            System.out.println("生日格式错误，请使用正确的格式（月月-日日）。");
        }

        // 入校日期计算功能
        System.out.println("请输入入校日期（格式：yyyy-MM-dd）：");
        String enrollmentDateStr = scanner.nextLine();
        try {
            LocalDate enrollmentDate = LocalDate.parse(enrollmentDateStr, fullFormatter);
            long years = ChronoUnit.YEARS.between(enrollmentDate, today);
            long months = ChronoUnit.MONTHS.between(enrollmentDate, today) % 12;
            long days = ChronoUnit.DAYS.between(enrollmentDate, today) - years * 365 - months * 30;

            System.out.println("自入校以来：");
            System.out.println(years + "年，" + months + "月，" + days + "天");
        } catch (Exception e) {
            System.out.println("入校日期格式错误，请使用正确的格式（yyyy-MM-dd）。");
        }

        scanner.close();
    }
}